<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\WebApi\Business\ConfigBusiness;
use App\Services\Netease\UserServices as NetUserServices;
use Illuminate\Http\Request;
use App\Models\ImGroup;
use App\Models\ImRole;
use App\Models\ImGroupUser;
use App\Models\ImGroupBlack;
use App\Models\ImGroupBanned;



class GroupController
{


    /**
     * @desc 群组管理
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:44
     */
    public function index(Request $request, ImGroup $ImGroupModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $query = $ImGroupModel->where(['status'=>0, 'user_id'=>$userId]);
        $list = $query->select('id','im_gid','user_id','face','name','description','max_count','users_count')->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        return okBack($list);
    }

    /**
     * @desc 角色列表
     * @param Request $request
     * @param ImRole $ImRoleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:53
     */
    public function roleList(ImRole $ImRoleModel)
    {
        $list = $ImRoleModel->get();

        return okBack($list);
    }

    /**
     * @desc 获取一个用户参与的所有群组
     * @param Request $request
     * @param ImRole $ImRoleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:53
     */
    public function userList(Request $request, ImGroup $ImGroupModel, ImRole $ImRoleModel, ImGroupUser $ImGroupUserModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImGroupUserModel->select('id', 'im_gid', 'user_id', 'role', 'status')->with(['group' => function($q){
            $q->select('im_gid','name','description','face');
        }])->with(['role' => function($q){
            $q->select('id','name');
        }])->where(['user_id'=>$userId])->whereIn('status', [0,3])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        return okBack($list);
    }

    /**
     * @desc 获取群组详情
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 14:25
     */
    public function detail(Request $request, ImGroup $ImGroupModel)
    {
        $imGid= $request->input('im_gid') ?? 0;
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupModel->detail($imGid, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['data']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 创建群组
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:39
     */
    public function addGroup(Request $request, ImGroup $ImGroupModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupModel->addGroup($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack('添加成功', $res['data']);
        }
        else
        {
            return errBack('添加失败');
        }
    }

    /**
     * @desc 编辑群组信息
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:39
     */
    public function editGroup(Request $request, ImGroup $ImGroupModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupModel->editGroup($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 删除群组
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:58
     */
    public function delGroup(Request $request,ImGroup $ImGroupModel)
    {
        $imGid= $request->input('im_gid');
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupModel->delGroup($imGid, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群组群员管理
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:44
     */
    public function memberList(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $imGid= $request->input('im_gid');
        $user = $request->user();
        $userId = $user->id;
        $list = $ImGroupUserModel->memberList($imGid, $userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 添加群组成员
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:39
     */
    public function addSingleMember(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->addSingleMember($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg'], $res['data']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 批量添加群组成员
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:39
     */
    public function addManyMember(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->addManyMember($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg'], $res['data']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 移除群组成员单个
     * @param Request $request
     * @param ImGroupUser $ImGroupUserModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 9:00
     */
    public function delSingleMember(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->delSingleMember($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 批量移除群组成员
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:39
     */
    public function delManyMember(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->delManyMember($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群组管理员列表
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:44
     */
    public function managerList(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $imGid= $request->input('im_gid');
        $user = $request->user();
        $userId = $user->id;
        $list = $ImGroupUserModel->managerList($imGid, $userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 升级群管理员
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 15:39
     */
    public function upManager(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->upManager($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 移除群管理员
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 15:39
     */
    public function downManager(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->downManager($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 转让群主
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 15:39
     */
    public function turnOwner(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->turnOwner($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群组黑名单管理
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:44
     */
    public function blackList(Request $request, ImGroupBlack $ImGroupBlackModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $imGid= $request->input('im_gid');
        $user = $request->user();
        $userId = $user->id;
        $list = $ImGroupBlackModel->blackList($imGid, $userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 添加用户到群组黑名单
     * @param Request $request
     * @param ImGroupBlack $ImGroupBlackModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 11:05
     */
    public function addSingleBlack(Request $request, ImGroupBlack $ImGroupBlackModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupBlackModel->addSingleBlack($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 添加多个户到群组黑名单
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:39
     */
    public function addManyBlack(Request $request, ImGroupBlack $ImGroupBlackModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupBlackModel->addManyBlack($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 移除单个黑名单
     * @param Request $request
     * @param ImGroupUser $ImGroupUserModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 9:00
     */
    public function delSingleBlack(Request $request, ImGroupBlack $ImGroupBlackModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupBlackModel->delSingleBlack($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 移除多个黑名单用户
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:39
     */
    public function delManyBlack(Request $request, ImGroupBlack $ImGroupBlackModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupBlackModel->delManyBlack($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群组批量添加禁言
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 15:39
     */
    public function addManyBanned(Request $request, ImGroupBanned $ImGroupBannedModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupBannedModel->addManyBanned($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群组批量解除禁言
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:39
     */
    public function freeManyBanned(Request $request, ImGroupBanned $ImGroupBannedModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupBannedModel->freeManyBanned($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群组禁言列表管理
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:44
     */
    public function bannedList(Request $request, ImGroupBanned $ImGroupBannedModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $imGid= $request->input('im_gid');
        $user = $request->user();
        $userId = $user->id;
        $list = $ImGroupBannedModel->bannedList($imGid, $userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 群管和群主退出群聊
     * @param Request $request
     * @param ImGroupUser $ImGroupUserModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 9:00
     */
    public function outGroup(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->outGroup($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群管和群主修改群名称
     * @param Request $request
     * @param ImGroupUser $ImGroupUserModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 9:00
     */
    public function setGroupName(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->setGroupName($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群管和群主修改入群许可
     * @param Request $request
     * @param ImGroupUser $ImGroupUserModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 9:00
     */
    public function setGroupAudit(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->setGroupAudit($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群管和群主修改设置全员禁言
     * @param Request $request
     * @param ImGroupUser $ImGroupUserModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 9:00
     */
    public function setAllBanned(Request $request,  ImGroup $ImGroupModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupModel->setAllBanned($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 扫码添加单个群组成员
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:39
     */
    public function addScanMember(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->addScanMember($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 添加群组成员
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:39
     */
    public function auditMember(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->auditMember($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群组
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:44
     */
    public function memberLoginList(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $imGid= $request->input('im_gid');
        $user = $request->user();
        $userId = $user->id;
        $list = $ImGroupUserModel->memberLoginList($imGid, $userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 邀请邀请确认列表
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/6 10:44
     */
    public function inviteConfirmList(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $imGid= $request->input('im_gid');
        $user = $request->user();
        $userId = $user->id;
        $list = $ImGroupUserModel->inviteConfirmList($imGid, $userId);

        return okBack($list);
    }

    /**
     * @desc 邀请详情
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/7 10:44
     */
    public function inviteConfirmDetail(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $list = $ImGroupUserModel->inviteConfirmDetail($input, $userId);

        return okBack($list);
    }

    /**
     * @desc 群组群员管理
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:44
     */
    public function roleMemberList(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $imGid= $request->input('im_gid');
        $user = $request->user();
        $userId = $user->id;
        $list = $ImGroupUserModel->roleMemberList($imGid, $userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 群组群聊列表
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:44
     */
    public function chatList(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImGroupUserModel->chatList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 平台的聊天记录存入数据库
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:44
     */
    public function chatRecord(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $list = $ImGroupUserModel->chatRecord();

        return okBack($list);
    }




}
